/**
 * 
 */
package cn.edu.bjtu.mvc;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.deeplearning4j.models.embeddings.wordvectors.WordVectors;
import org.deeplearning4j.text.tokenization.tokenizerfactory.TokenizerFactory;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.edu.bjtu.configuration.TextCategorizationCNNConfig;
import cn.edu.bjtu.core.CNNDataSetIteratorProviderHandler;
import cn.edu.bjtu.core.LoggerSupport;
import cn.edu.bjtu.datasource.dsiter.LengthExceptionDetectCNNDataSetIterator;
import cn.edu.bjtu.datasource.lsp.FDLabeledSentenceExceptionSupportProvider;
import cn.edu.bjtu.mvc.ee.AsyncInvoke;
import pojo.ResCode;


/**
 * @author zhangzhidong
 *
 */
public abstract class BaseController  extends LoggerSupport implements CNNDataSetIteratorProviderHandler{
	private static DiskFileItemFactory dfif = new DiskFileItemFactory();
	private static ServletFileUpload sfu = new ServletFileUpload(dfif);
	protected static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	protected static ObjectMapper mapper  = new ObjectMapper();
	protected ApplicationEventPublisher applicationEventPublisher;
	static{
		mapper.setDateFormat(sdf);
	}
	public static final ResCode SUCCESS = new ResCode(0,"success");
	public static final ResCode ERROR = new ResCode(400,"error");
	public static final ResCode ParamsError = new ResCode(400,"params error");

	protected ModelAndView renderView(String viewName,Map<String,?> model) {
		return new ModelAndView(viewName, model);
	}
	protected ModelAndView renderView(String viewName,String name,Object obj) {
		return new ModelAndView(viewName, name, obj);
	}
	
	protected void downloadFile(HttpServletRequest req,HttpServletResponse res) {
		
	}
	protected String jsonDump(Object object) {
		try {
			return mapper.writeValueAsString(object);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return jsonDump(ERROR);
		}
	}
	protected <T> T fromJson(String content,Class<T> clz){
		try {
			return mapper.readValue(content, clz);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 如果子类不重新提供训练集与测试集,就在基类中返回默认的,也就是配置文件中的
	 */
	@AsyncInvoke(by="TextCategorizationCNNModel中线程池中的线程")
	@Override
	public DataSetIterator handleTrain(TextCategorizationCNNConfig config, WordVectors wv, TokenizerFactory tf,
			int batch, int senLen) throws Exception {
		FDLabeledSentenceExceptionSupportProvider fsd = new FDLabeledSentenceExceptionSupportProvider(config.getDataSetDirOrFile());
		return new LengthExceptionDetectCNNDataSetIterator.Builder()
				.tokenizerFactory(tf)
		        .sentenceProvider(fsd)
		        .wordVectors(wv)
		        .minibatchSize(batch)
		        .maxSentenceLength(senLen)
		        .useNormalizedWordVectors(false)
		        .build();
	}
	@AsyncInvoke(by="TextCategorizationCNNModel中线程池中的线程")
	@Override
	public DataSetIterator handleTest(TextCategorizationCNNConfig config, WordVectors wv, TokenizerFactory tf,
			int batch, int senLen) throws Exception {
		FDLabeledSentenceExceptionSupportProvider fsd = new FDLabeledSentenceExceptionSupportProvider(config.getTestDataSetDirOrFile());
		return new LengthExceptionDetectCNNDataSetIterator.Builder()
				.tokenizerFactory(tf)
		        .sentenceProvider(fsd)
		        .wordVectors(wv)
		        .minibatchSize(batch)
		        .maxSentenceLength(senLen)
		        .useNormalizedWordVectors(false)
		        .build();
	}

	
	
}

